# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

# gtk3 support is still experimental
# It can be tested via _use_gtk3=yes
_use_gtk3=no
if [ "${_use_gtk3}" = "yes" ]; then
  _gtksuff=3
fi

_realname=inkscape
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.91
pkgrel=14
pkgdesc="Vector graphics editor using the SVG file format (mingw-w64)"
arch=('any')
url="http://inkscape.sourceforge.net/"
license=("GPL" "LGPL")
install=inkscape-${CARCH}.install
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-boost"
             "intltool")
depends=(
          "${MINGW_PACKAGE_PREFIX}-aspell"
          "${MINGW_PACKAGE_PREFIX}-gc"
          "${MINGW_PACKAGE_PREFIX}-ghostscript"
          "${MINGW_PACKAGE_PREFIX}-gsl"
          "${MINGW_PACKAGE_PREFIX}-gtkmm${_gtksuff}"
          "${MINGW_PACKAGE_PREFIX}-gtkspell${_gtksuff}"
          "${MINGW_PACKAGE_PREFIX}-hicolor-icon-theme"
          "${MINGW_PACKAGE_PREFIX}-imagemagick"
          "${MINGW_PACKAGE_PREFIX}-lcms2"
          "${MINGW_PACKAGE_PREFIX}-libcdr"
          "${MINGW_PACKAGE_PREFIX}-libxml2"
          "${MINGW_PACKAGE_PREFIX}-libxslt"
          "${MINGW_PACKAGE_PREFIX}-libwpg"
          "${MINGW_PACKAGE_PREFIX}-poppler"
          "${MINGW_PACKAGE_PREFIX}-popt"
          "${MINGW_PACKAGE_PREFIX}-python2"
          $([[ "${_use_gtk3}" == "yes" ]] && echo \
            "${MINGW_PACKAGE_PREFIX}-gdl" ) \
        )
optdepends=(#"${MINGW_PACKAGE_PREFIX}-pstoedit: latex formulas"
            #"${MINGW_PACKAGE_PREFIX}-texlive-core: latex formulas"
            "${MINGW_PACKAGE_PREFIX}-python2-numpy: some extensions"
            "${MINGW_PACKAGE_PREFIX}-python2-lxml: some extensions and filters"
            #"${MINGW_PACKAGE_PREFIX}-uniconvertor: reading/writing to some proprietary formats"
            )
options=('staticlibs' 'strip')
source=("https://inkscape.global.ssl.fastly.net/media/resources/file/${_realname}-${pkgver}.tar.gz"
        inkscape-0.48.4-headers-fix.patch
        inkscape-0.48.4-python.patch
        inkscape-0.48.4-use-GStatBuf-insted-stat.patch
        inkscape-0.48.4-use-GStatBuf-insted-stat-gtk3.patch
        inkscape-0.48.4-poppler-includes-conflicts.patch
        inkscape-0.48.4-win32-relocatable.patch
        inkscape-0.48.4-win32-file-dialog.patch
        inkscape-0.48.4-python-extensions-location.patch
        inkscape-0.91-simulaneous-intltool-gettext.patch)
sha256sums=('2ca3cfbc8db53e4a4f20650bf50c7ce692a88dcbf41ebc0c92cd24e46500db20'
            'e578fbef476704a42522ec5298f0202d2e600c2e03a98dba2c62d4ee1e53b6b5'
            'ee948c90126407285b262cbea3d5a182e8fc166fddce09ea33d689fde3101a49'
            '53351468636dec1b00815d87d29dc1117538378f691efd00d2ef4716e0153ed9'
            'a6013a43d26a0cbb6cfc19d1c7ce7d63c24b2b9368c22df600f0cf8ad741ff1e'
            '7458f4fac3bf75d388b71a61bdc30e0bed97cbbf63b73119b2f61af6d674a8b0'
            'e68cf8dfd575639923527450b6357eda8e5656fdca18acdd0471d8cc429f7b16'
            '5912d39285b0fd4ab63651836ef6ddd32d149a1e739c9561584fdcadc0b17220'
            '55b0922f98c98ac5df8ba0869c46ff9eeeee9aaceef9afbaad9c4763dd3923ba'
            'ea625d1d7260d32866e4e61946fa02793b1762f66dc223364dbcc5e628bb5bfe')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i ${srcdir}/inkscape-0.48.4-headers-fix.patch
  patch -p1 -i ${srcdir}/inkscape-0.48.4-python.patch
  if [ "${_use_gtk3}" = "no" ]; then
    patch -p1 -i ${srcdir}/inkscape-0.48.4-use-GStatBuf-insted-stat.patch
  else
    patch -p1 -i ${srcdir}/inkscape-0.48.4-use-GStatBuf-insted-stat-gtk3.patch
  fi
  patch -p1 -i ${srcdir}/inkscape-0.48.4-poppler-includes-conflicts.patch
  patch -p1 -i ${srcdir}/inkscape-0.48.4-win32-relocatable.patch
  patch -p1 -i ${srcdir}/inkscape-0.48.4-win32-file-dialog.patch
  patch -p1 -i ${srcdir}/inkscape-0.48.4-python-extensions-location.patch
  patch -p1 -i ${srcdir}/inkscape-0.91-simulaneous-intltool-gettext.patch

  sed -i 's|/usr/bin/python\>|/usr/bin/python2|g' cxxtest/*.py
  sed -i 's|/usr/bin/env python\>|/usr/bin/env python2|g' share/*/{test/,}*.py
  #sed -i 's|"python" },|"python2" },|g' src/extension/implementation/script.cpp
  sed -i 's|python -c|python2 -c|g' configure share/extensions/uniconv*.py
  sed -i '/^#include <g.kmm/i #include <glibmm.h>' src/*{,/*{,/*{,/*}}}.{h,cpp}
  sed -e "s|export-dynamic|export-all-symbols|g" -i src/Makefile.am

  autoreconf -fi -I m4
}

build() {
  # export lt_cv_deplibs_check_method='pass_all'
  [[ -d build-${CARCH} ]] && rm -rf build-${CARCH}
  mkdir -p build-${CARCH} && cd build-${CARCH}

  declare -a extra_config
  if [ "${_use_gtk3}" = "yes" ]; then
    extra_config+=(--enable-gtk3-experimental)
  fi

  CXXFLAGS+=" -std=gnu++11"
  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --enable-lcms \
    --enable-poppler-cairo \
    --disable-dependency-tracking \
    --disable-strict-build \
    ${extra_config[@]}

  make
}

package() {
  cd ${srcdir}/build-${CARCH}
  make DESTDIR="${pkgdir}" install
}
